//
//  LeaderActityManVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/31.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "LeaderActityManVC.h"
#import "LenderActityManTopCell.h"
#import "LeaderActityCell.h"
#import "ActivityDetailVC.h"
@interface LeaderActityManVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LeaderActityManVC
{
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
    
    NSString *_statusStr;
    UIButton *_changeBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动报名";
    self.view.backgroundColor = superBackgroundColor;
    [self requsetForActitySignList];
    [self initTopView];
    [self initTableView];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)initTopView{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 35)];
    topView.backgroundColor = superBackgroundColor;
    [self.view addSubview:topView];
    UILabel *listLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 100, 25)];
    listLb.text = @"报名列表";
    listLb.font = [UIFont systemFontOfSize:14];
    listLb.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:listLb];
    UIButton *detailBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 100, 0, 100, 35)];
    [detailBtn setTitle:@"查看活动详情" forState:UIControlStateNormal];
    [detailBtn addTarget:self action:@selector(detailAction) forControlEvents:UIControlEventTouchUpInside];
    [detailBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [detailBtn setTitleColor:Theme_Color forState:UIControlStateNormal];
    [topView addSubview:detailBtn];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 99, KScreenWidth - 20, KScreenHeight - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    _changeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, KScreenHeight-45, KScreenWidth, 45)];
    _changeBtn.backgroundColor = Theme_Color;
    if ([self.status isEqualToString:@"1"]) {
        [_changeBtn setTitle:@"开始活动" forState:UIControlStateNormal];
        _statusStr = @"2";
        [_changeBtn addTarget:self action:@selector(changeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    if ([self.status isEqualToString:@"2"]) {
        [_changeBtn setTitle:@"结束活动" forState:UIControlStateNormal];
        _statusStr = @"3";
        [_changeBtn addTarget:self action:@selector(changeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    if ([self.status isEqualToString:@"3"]) {
        [_changeBtn setTitle:@"已结束" forState:UIControlStateNormal];
    }
    
    [self.view addSubview:_changeBtn];
}
#pragma - mark RequestForChangeStatus
- (void)changeBtnAction{
    //参数
    NSDictionary *parameters = @{
                                 @"activityID":self.actityId,
                                 @"status": _statusStr,
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/updateActivityStatus.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            _changeBtn.backgroundColor = Gray_Color;
            if ([self.status isEqualToString:@"1"]) {
                [_changeBtn setTitle:@"活动已开始" forState:UIControlStateNormal];
            }
            if ([self.status isEqualToString:@"2"]) {
                [_changeBtn setTitle:@"活动已结束" forState:UIControlStateNormal];
            }
            [_changeBtn removeTarget:self action:@selector(changeBtnAction) forControlEvents:UIControlEventTouchUpInside];
            [_changeBtn addTarget:self action:@selector(laterBtn) forControlEvents:UIControlEventTouchUpInside];
            [self performSelector:@selector(backToLastCtr) withObject:nil afterDelay:1];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"失败"];
    }];
}
- (void)backToLastCtr {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)laterBtn{
    //
}
#pragma - mark RequsetForActitySignList
- (void)requsetForActitySignList{
    //参数
    NSDictionary *parameters = @{
                                 @"activityID":self.actityId,
                                 @"isMember":@"",
                                 @"userId": @"",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/signUpPersonList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //请求成功
//            [SVProgressHUD showSuccessWithStatus:@"请求成功"];
            [_tableView reloadData];
            
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求报名列表失败"];
    }];
}
#pragma - mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count+2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        LenderActityManTopCell *cell = [[NSBundle mainBundle]loadNibNamed:@"LenderActityManTopCell" owner:nil options:nil][0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == _responseArr.count+1) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        UILabel *totalNumLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth - 130, 0, 100, 35)];
        totalNumLb.text = [@"已报名人数：" stringByAppendingString:[NSString stringWithFormat:@"%lu",(unsigned long)_responseArr.count]];
        totalNumLb.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:totalNumLb];
        return cell;
    }else{
        LeaderActityCell *cell = [[NSBundle mainBundle]loadNibNamed:@"LeaderActityCell" owner:nil options:nil][0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.name.text = _responseDic[@"list"][indexPath.row-1][@"name"];
        cell.phone.text = _responseDic[@"list"][indexPath.row-1][@"userPhone"];
        cell.roomNum.text = [NSString stringWithFormat:@"%@",_responseDic[@"list"][indexPath.row-1][@"roomCount"]];
        return cell;
    }
    return nil;
}
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return UITableViewCellEditingStyleDelete;
//}
//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (editingStyle==UITableViewCellEditingStyleDelete) {
//
//    }
//}
//
- (void)detailAction{
    ActivityDetailVC *vc = [[ActivityDetailVC alloc]init];
    vc.activityHtmlUrlStr = self.htmlUrl;
    vc.activityId = self.actityId;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
